/**
 * 用户列表页面
 */
var userList = function() {
    var self = this;
    PageList.call(self);

    self.table = null;
    self.userInput = null;
    self.realnameInput = null;
    self.qqInput = null;
    self.gradeSelect = null;
    self.stateSelect = null;
    self.ipInput = null;
    self.dayInput = null;
    self.sortSelect = null;
    self.admin = false;
};
userList.prototype = Object.create(PageList.prototype);

userList.prototype.init = function() {
    var self = this;

    // 填充用户组
    if (self.gradeSelect) {
        for (var i = 1; i < 50; i++) {
            var d = Rights.roles[i + ''];
            if (!d) continue;
            self.gradeSelect.append("<option value='" + i + "'>" + d + '</option>');
        }
    }
    
    self.jumpPage = function(page) {
        self.refresh(page);
    };
    self.refresh(1);
}

userList.prototype.refresh = function(page) {
    var self = this;
    self.currPage = page;
    var uid = undefined;
    if (self.userInput) {
        uid = self.userInput.val().trim();
        if (uid == '') uid = undefined;
    }
    if (uid) {
        startLoading();
        CMD.find_user_by_name(uid, function(r) {
            stopLoading();
            self.list = r;
            if (self.customFillResults) {
                self.fillPages(r.count, r.number, r.page, r.size);
                self.customFillResults(r.list);
                return;
            }
            self.fillResults(r);
        });
        return;
    }

    var ip = undefined;
    if (self.ipInput) {
        ip = self.ipInput.val().trim();
        if (ip == '') ip = undefined;
    }

    var qq = undefined;
    if (self.qqInput) {
        qq = self.qqInput.val().trim();
        if (qq == '') qq = undefined;
    }

    var name = undefined;
    if (self.realnameInput) {
        name = self.realnameInput.val().trim();
        if (name == '') name = undefined;
    }

    var state = undefined;
    if (self.stateSelect) {
        state = self.stateSelect.val();
        if (state == -1) state = undefined;
    }
    if (self.admin) state = 0;

    var loginDay = undefined;
    if (self.dayInput) {
        loginDay = self.dayInput.val();
        if (loginDay == '') loginDay = undefined;
        else loginDay = parseInt(loginDay);
        if (loginDay < 0) loginDay = 0;
    }

    var sort = undefined;
    if (self.sortSelect) {
        sort = self.sortSelect.val();
    }

    var data = {

    };
    if (uid) data.uid = uid;
    if (ip) data.ip = ip;
    if (qq) data.qq = qq;
    if (name) data.name = name;
    if (state !== undefined) data.state = state;
    if (loginDay !== undefined) data.loginDay = loginDay;
    if (sort !== undefined) data.sort = sort;
    if (self.online) data.online = 1; 
    
    if (!self.gradeSelect || self.gradeSelect.val() == '-1') {
        if (self.admin) data.grade = "50,99";
        else data.grade = "0,49";
    }
    else if (self.gradeSelect) {
        if (self.gradeSelect.val() == '1')
            data.grade = "0,1";
        else
            data.grade = self.gradeSelect.val() + ',' + self.gradeSelect.val();
    }

    // 检索数据
    startLoading();
    CMD.get_user_list(data, page, self.getPageSize(), function(r) {
        stopLoading();
        self.list = r;
        if (self.customFillResults) {
            self.fillPages(r.count, r.number, r.page, r.size);
            self.customFillResults(r.list);
            return;
        }
        self.fillResults(r);
    });
}

userList.prototype.fillResults = function(list) {
    var self = this;
    self.table.empty();
    self.fillPages(list.count, list.number, list.page, r.size);

    var list = list.list;
    var ips = [];
    for (var i in list) {
        var d = list[i];
        var account = {
            deposit: 0,
            withdraw: 0,
            dw: 0,
        };
        for (var j in d.account) {
            if (d.account[j].owner == "") {
                account = d.account[j];
                break;
            }
        }

        var tr = $("<tr></tr>");
        var name = '<span class="gray">' + d.mobile + '</span>';
        if (d.online == 1)
            name = '<span class="blue">' + d.mobile + '</span>';
        tr.append("<td class='name'>" + name + "</td>");
        tr.append("<td>" + (d.name || '') + "</td>");

        // 用户层级
        var l = '<td><span class="am-icon-edit layer" value="__v__">未分层</span></td>';
        l = l.replace(/__v__/, d.mobile);
        tr.append(l);
        //tr.append("<td>" + ((Rights.roles[d.grade || "1"]) || '超级管理员') + "</td>");
        tr.append("<td>" + (d.balance/100).toFixed(2) + "</td>");
        tr.append("<td>" + ((d.depositSum || 0)/100).toFixed(2) + "</td>");
        tr.append("<td>" + ((d.withdrawSum || 0)/100).toFixed(2) + "</td>");
        tr.append("<td>" + (account.deposit/100) + "</td>");
        tr.append("<td>" + (account.withdraw/100) + "</td>");
        tr.append("<td>" + (d.depositCount || 0) + "/" + (d.withdrawCount || 0) + "</td>");
        tr.append("<td>" + (account.dw/100) + "</td>");
        tr.append("<td>" + self.addOper(i) + "</td>");
        if (self.online) {
            tr.append("<td>" + (d.source || '') + "</td>");
            tr.append("<td>" + (d.url || '') + "</td>");
        }
        tr.append("<td class='ip'>" + (d.ip || '') + "</td>");
        tr.append("<td>" + (d.state == 0 ? "正常" : "冻结") + "</td>");
        self.table.append(tr);

        if (d.ip && ips.indexOf(d.ip) == -1) ips.push(d.ip);
    }

    for (var i in ips) self.fillIpInfo(ips[i]);

    var arr = $("td.name");
    for (var i = 0; i < arr.length; i++) {
        self.registerPopover($(arr[i]), i);
    }
    
    $("td button.edit").click(function(e) {
        // 编辑用户信息
        self.currUser = list[parseInt($(e.currentTarget).attr("value"))];
        Util.addPopUp("user_info", "user_info", 390);
    });
    $("td button.signout").click(function(e) {
        self.currUser = list[parseInt($(e.currentTarget).attr("value"))];
        CMD.force_signout(self.currUser.id, function() {
            self.refresh(self.currPage);
        });
    });
    $("td button.remove").click(function(e) {
        self.currUser = list[parseInt($(e.currentTarget).attr("value"))];
        if (confirm("确定是否删除此管理员账号？")) {
            CMD.user_state(self.currUser.id, 1, function() {
                self.refresh(self.currPage);
            })
        }
    });
}

userList.prototype.registerPopover = function(td, index) {
    var self = this;
    var user = self.list.list[index];
    td.dblclick(function() {
        show_user_detail(user);
    });
}

userList.prototype.fillIpInfo = function(ip) {
    CMD.ip_info(ip, function(r) {
        var arr = $("td.ip");
        for (var i = 0; i < arr.length; i++) {
            var td = $(arr[i]);
            if (td.html() != ip) continue;
            td.html(r.addr);
        }
    });
}

userList.prototype.addOper = function(index) {
    var self = this;
    var data = self.list.list[index];
    var oper = '<button class="edit" value="__v__">修改</button>\
        <button class="add_balance" value="__v__">余额变更</button> \
        <button class="bet" value="__v__">注单明细</button> \
        <button class="balance" value="__v__">资金明细</button> \
        <select class="more" value="__v__" denyau="1"> \
            <option value="-1" selected>更多</option>\
            <option value="detail">查看详情</option>\
            <option value="modify_name">修改名字</option>\
            <option value="dw">打水编辑</option>\
            <option value="lottery">盘口设定</option>\
            <option value="delete">删除会员</option>\
            <option value="daili">更换代理</option>\
            <option value="layer">锁定层级</option>\
        </select>\
    ';
    oper = oper.replace(/__v__/g, data.mobile);
    return oper;
}

window.UserList = new userList();
